package bsserver;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Vector;

/**
 * Encapsulated Request URI object to handle resource locating/existence.
 * @author tblackstone
 */
public class RequestURI {

    private URI uri;
    private boolean exists = false;
    private boolean isScript = false;
    private String absolutePath;
    private String pathInfoString;
    private String queryString;

    /**
     * Creates a new RequestURI object by parsing the given request string, per
     * RFC 2396.
     * @param requestString the URI to parse
     * @throws java.net.URISyntaxException
     */
    public RequestURI(String requestString) throws URISyntaxException {

        uri = new URI(requestString);

        absolutePath = Main.HttpdConfig.absolutePath(uri.getPath());
        
        isScript = Main.HttpdConfig.isScript(uri.getPath());

        if (uri.getQuery() != null) {
            queryString = uri.getQuery();
        } else {
            queryString = "";
        }

        if (isScript) {
            // Parse path info string
            pathInfoString = Main.HttpdConfig.extraPathInfo(uri.getPath());

            exists = true;

        } else {

            File resource = new File(absolutePath);

            if (resource.isDirectory()) {
                if (!absolutePath.endsWith(File.separator)) {
                    absolutePath = absolutePath + File.separator;
                }
                resource = null;
                Vector<String> indexDocs = Main.HttpdConfig.getDirectoryIndex();
                for (String index : indexDocs) {
                    File candidate = new File(absolutePath + index);
                    if (candidate.isFile()) {
                        resource = candidate;
                        absolutePath = resource.getAbsolutePath();
                        exists = true;
                        break;
                    }
                }
            } else if (resource.isFile()) {
                exists = true;
            }
        }

    }

    /**
     * Gets the absolute system path to this URI
     * @return the absolute path
     */
    public String getAbsolutePath() {
        return absolutePath;
    }

    /**
     * Gets the query string portion of this URI
     * @return the query string
     */
    public String getQueryString() {
        return queryString;
    }

    /**
     * Gets the path info string portion of this URI
     * @return the path info string
     */
    public String getPathInfoString() {
        return pathInfoString;
    }

    /**
     * Gets the raw URI string (with escaped encoding, if present)
     * @return the raw URI
     */
    public String getURIString() {
        return uri.getRawPath();
    }

    /**
     * Checks if the resource referred to by this URI is a CGI script as defined
     * by server directives.
     * @return true if this URI points to a CGI script, false otherwise
     */
    public boolean isScript() {
        return isScript;
    }

    /**
     * Checks if the resource referred to by this URI exists.
     * @return true if the resource exists, false otherwise
     */
    public boolean resourceExists() {
        return exists;
    }
}
